<?
 require_once("../core.php");

 @get_all_variables();
 $db = dbconnect();

 $q = "SELECT *
       FROM `clients`
       WHERE 1
       ORDER BY `id` ASC";

 $r = mysql_query($q, $db) or die(mysql_error());

 function testsByProblemId($problem_id, $id) {
  $db = dbconnect();
  $q = "SELECT *
        FROM `submits`
        WHERE (problem_id=\"$problem_id\")AND(client_id=".$id.")
        ORDER BY `id` DESC";

  $r = mysql_query($q, $db) or die (mysql_error());
  $n = 0;
  @$n = mysql_num_rows($r);
  if ($n == 0) {
      return 0;
  } else
  {
      $wx = mysql_fetch_array($r);
      return $wx['test'];
  }
 }
?>

<table id="table" border="0" width="100%">
            <thead>
                <tr>
                    <th width="300">Team</th>
                    <th>Score</th>
                    <th>Submits</th>
                    <th>A</th>
                    <th>B</th>
                    <th>C</th>
                    <th>D</th>
                    <th>E</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                <?
                  $n = mysql_num_rows($r);
                  for ($i=0; $i<$n;$i++) {
                      $w = mysql_fetch_array($r);
                   $q2 = "SELECT *
                          FROM `submits`
                          WHERE client_id = ".$w['id'];
                   $r2 = mysql_query($q2, $db) or die(mysql_error());
                   $submits = mysql_num_rows($r2);
                   $A = testsByProblemId("A", $w['id']);
                   $B = testsByProblemId("B", $w['id']);
                   $C = testsByProblemId("C", $w['id']);
                   $D = testsByProblemId("D", $w['id']);
                   $E = testsByProblemId("E", $w['id']);
                ?>
                    <td><? echo "<a href=\"client.php?id=".$w['id']."\" rel=\"facebox\">".$w['title']."</a>"; ?></td>
                    <td><? echo $A+$B+$C+$D+$E; ?></td>
                    <td><? echo $submits; ?></td>
                    <td><? echo $A; ?></td>
                    <td><? echo $B ?></td>
                    <td><? echo $C; ?></td>
                    <td><? echo $D; ?></td>
                    <td><? echo $E; ?></td>
                </tr>
                <? }; ?>
            </tbody>
        </table>